﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Small.Html.Entities;

namespace Small.Html.UI
{
  public static  class ExpressConvert
  {
      
      /// <summary>
      /// 扩展Object类,用于解析Labmda表达式是获取到说明
      /// </summary>
      /// <param name="obj"></param>
      /// <param name="name说明"></param>
      /// <returns></returns>
      public static string Desc(this object obj, string desc) 
      {
          return desc;
      }



      /// <summary>
      /// 表格中html内容
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="obj"></param>
      /// <param name="htmlFormat"></param>
      /// <param name="args"></param>
      /// <returns></returns>
      public static string ColumnHtml<T>(this object obj, string htmlFormat, params Expression<Func<T, object>>[] args) 
      {
          if (String.IsNullOrEmpty(htmlFormat)) { return null; }
          StringBuilder builder = new StringBuilder();
          List<String> argsList = new List<string>();
          foreach (var item in args)
          {
              ExpressItem expressItem = BaseExpress.GetExpressName<T>(item);
              argsList.Add(String.Format("${{0}}",expressItem.Text));
          }
          var formatList = htmlFormat.Split('}');
          if (formatList.Count() != argsList.Count) {
              throw new ArgumentOutOfRangeException("参数不一致");
          }
          for (int i = 0; i < formatList.Count(); i++)
          {
             builder.Append(Regex.Replace(formatList[i],"{\\d}",argsList[i]));
          }
          return builder.ToString();
            

      }
  }
}
